package com.example.service.impl;

import com.example.entity.User;
import com.example.entity.UserVO;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.example.service.AdminService;
import com.example.util.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**登录
     *
     */
    public UserVO login(User user){
        User dbAdmin =adminMapper.selectByUserid(user.getId());
        if(dbAdmin == null ){
            //没有用户
            throw new CustomException("账号或密码错误1");
        }
        //比较密码
        if(!user.getPassword().equals(dbAdmin.getPassword())){
            throw new CustomException("账号或密码错误2");
        }
        //登录成功
        else {
            String token = JwtUtil.createToken(dbAdmin.getName(), dbAdmin.getRole(), dbAdmin.getPassword());
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(dbAdmin,userVO);
            userVO.setToken(token);
            return userVO;
        }



    }

/**注册
     *
     */
    @Override
    public void register(User user) {

        adminMapper.insert(user);
    }

    /**更新
     *-cyp
     */
    @Override
    public void updateById(User user) {
        adminMapper.updateById(user);
    }


}
